#!/bin/bash
PROG=@CMAKE_INSTALL_PREFIX@/llvmbin/@clang_compiler@
FLAGS="-target @TARGET_CPU@-pc-windows-gnu"
FLAGS+=" --sysroot @MINGW_INSTALL_PREFIX@"
FLAGS+=" -fuse-ld=lld --ld-path=@TARGET_ARCH@-ld"
FLAGS+=" -Wno-unused-command-line-argument -Wno-int-conversion -Wno-packed -Wno-unused-function -Wno-macro-redefined -Wno-backend-plugin -Wno-c++11-narrowing"
FLAGS+=" -D_WIN32_WINNT=0x0A00 -DWINVER=0x0A00 -DNDEBUG -UHAS_80_BIT_LONG_DOUBLE -DHAS_80_BIT_LONG_DOUBLE=0 -U__USE_MINGW_ANSI_STDIO -D__USE_MINGW_ANSI_STDIO=0 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0"
FLAGS+=" -Xclang -fno-pch-timestamp -Xclang -mlong-double-64 -fsplit-lto-unit -fno-auto-import -fno-temp-file -fno-ident -mno-stack-arg-probe"
FLAGS+=" -ffile-prefix-map=@MINGW_INSTALL_PREFIX@=@TARGET_ARCH@ -ffile-prefix-map=../= -fno-file-reproducible"
unset CCACHE

if [ "@TARGET_CPU@" == "aarch64" ]; then
    ARCH_FLAGS="-mcpu=@MARCH@ -mtune=@MTUNE@"
else
    ARCH_FLAGS="-march=@MARCH@ -mtune=@MTUNE@"
fi

if [ "$_IS_CONFIGURE" == "1" ]; then
    SKIP_OPT="-g0 -O0 -fno-lto -fno-data-sections -fno-function-sections"
else
    if [ "$_CODEVIEW_ENABLED" != "0" ]; then
        FLAGS+=" -gcodeview -gcodeview-ghash -gno-codeview-command-line -Xclang -gsrc-hash=none"
    else
        FLAGS+=" -gdwarf -gsplit-dwarf=single -Xclang -gsrc-hash=none"
    fi
    if [ "@TARGET_CPU@" == "aarch64" ]; then
        FLAGS+=" -fveclib=SLEEF"
    else
        FLAGS+=" -fveclib=libmvec"
    fi
    FLAGS+=" -mguard=cf -O3 -fsized-deallocation -fnew-infallible -faligned-allocation -fstrict-vtable-pointers -fstrict-flex-arrays=3 -fstrict-aliasing -falign-functions=32 -funroll-loops -fno-signed-zeros -fno-trapping-math -freciprocal-math -fapprox-func -mrecip=all -ffp-contract=fast -fno-math-errno -fomit-frame-pointer -fmerge-all-constants -fno-unique-section-names -fdata-sections -fno-stack-protector -fslp-vectorize -ftree-vectorize -fvectorize -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-max-size=256"
    if [ "@ENABLE_CCACHE@" == "ON" ] && [ "$_NOCCACHE" != "1" ]; then
        CCACHE="ccache"
    fi
    if [ "$_PACKAGE_NAME" == "openlibm" ]; then
        FLAGS+=" -fsigned-zeros"
    fi
    if [ "$_GC_ENABLED" != "0" ]; then
        FLAGS+=" -ffunction-sections"
    fi
    if [ "$_LTO_ENABLED" != "0" ]; then
        LTO_FLAGS="-flto=thin -fwhole-program-vtables -funique-source-file-names"
    else
        FLAGS+=" @llvm_mllvm@ @polly@"
    fi
    if [ "@clang_compiler@" == "clang++" ]; then
        if [ "$_IS_EXCEPTIONS_ALLOWED" != "1" ]; then
            FLAGS+=" -fno-exceptions"
            if [ "$_IS_RTTI_ALLOWED" != "1" ]; then
                FLAGS+=" -fno-rtti"
            fi
            if [ "$_IS_UNWIND_ALLOWED" != "1" ]; then
                FLAGS+=" -fno-unwind-tables -fno-asynchronous-unwind-tables"
            fi
        fi
    else
        FLAGS+=" -fno-exceptions -fno-rtti"
        if [ "$_IS_UNWIND_ALLOWED" != "1" ]; then
            FLAGS+=" -fno-unwind-tables -fno-asynchronous-unwind-tables"
        fi
    fi
    if [ "@MARCH_HAS_AVX@" == "ON" ]; then
        FLAGS+=" -Wa,-msse2avx -mno-vzeroupper -mno-gather"
        if [ "@MARCH_HAS_AVX512@" == "ON" ]; then
            if [ "$_LTO_ENABLED" == "0" ]; then
                FLAGS+=" -mllvm -x86-use-fsrm-for-memcpy"
            fi
        fi
    fi
    if [ "$_LOCAL_EXEC_TLS" != "0" ]; then
        FLAGS+=" -ftls-model=local-exec"
    fi
    if [ "$_FORCE_HIDE_DLLEXPORT" == "1" ]; then
        FLAGS+=" -fvisibility-from-dllstorageclass -fvisibility-dllexport=hidden"
    fi
    if [ "$_NO_DEBUGINFO" != "1" ]; then
        FLAGS+=" -fdebug-info-for-profiling"
        if [ "$_FULL_DEBUGINFO" == "1" ]; then
            FLAGS+=" -g3"
        else
            FLAGS+=" -gline-tables-only"
        fi
    else
        FLAGS+=" -g0"
    fi
    if [ "@LLVM_ENABLE_PGO@" == "GEN" ] || [ "@LLVM_ENABLE_PGO@" == "CSGEN" ]; then
        export LLVM_PROFILE_FILE="@LLVM_PROFILE_DATA_DIR@/clang-%m.profraw"
    fi
    if [ "@CLANG_PACKAGES_PGO@" == "USE" ] && [ "$_PGO_ENABLED" != "0" ]; then
        PGO_FLAGS="-fprofile-use=@CLANG_PACKAGES_PROFDATA_FILE@"
    fi
    FLAGS+=" @CLANG_FLAGS@ $EXTRA_CFLAGS"
    FLAGS_INIT="$EXTRA_CFLAGS_INIT"
fi

if [ "@CLANG_PACKAGES_PGO@" == "GEN" ] && [ "$_PGO_ENABLED" != "0" ]; then
    PGO_FLAGS="-fprofile-generate="@CLANG_PACKAGES_PROFILE_DATA_DIR@" -fprofile-update=atomic -mllvm -vp-counters-per-site=64"
fi

if [ -n "$_PACKAGE_NAME" ] && [ -n "$_BINARY_DIR" ]; then
    FLAGS+="  -ffile-compilation-dir=. -Wa,-fdebug-compilation-dir=. -ffile-prefix-map=$_BINARY_DIR/=build/$_PACKAGE_NAME/ -ffile-prefix-map=@PKG_TO_SRC@/=soucre/ -ffile-prefix-map=@SINGLE_SOURCE_LOCATION@/=soucre/"
fi

filtered_args=()
if [ -n "$FILTER_FLAGS" ]; then
    read -r -a filter_flags_array <<< "$FILTER_FLAGS"
    for arg in "$@"; do
        skip=0
        for filter_flag in "${filter_flags_array[@]}"; do
            if [ "$arg" == "$filter_flag" ]; then
                skip=1
                break
            fi
        done
        if [ $skip -eq 0 ]; then
            filtered_args+=("$arg")
        fi
    done
else
    filtered_args=("$@")
fi

$CCACHE "$PROG" $LTO_FLAGS $PGO_FLAGS $ARCH_FLAGS $FLAGS_INIT "${filtered_args[@]}" $FLAGS $SKIP_OPT
